<?php

namespace app\admin\controller;

use think\facade\Log;
use think\db\Query;
use api\mall\model\IMSessionModel;
use cmf\controller\AdminBaseController;

class IMSessionController extends AdminBaseController
{
    public function index()
    {
        $filter = $this->request->param();
        $startTime = isset($filter['start_time']) ? $filter['start_time'] : '';
        $endTime = isset($filter['end_time']) ? $filter['end_time'] : '';
        if (!empty($startTime) && !empty($endTime) && $startTime >= $endTime) {
            // $this->error("截止日期需大于起始日期");
            $tempTime = $filter['start_time'];
            $filter['start_time'] = $filter['end_time'];
            $filter['end_time'] = $tempTime;
        }
        
        $model = new IMSessionModel();
        $query = $model->alias('cs');
        $query->leftJoin('user u1', 'cs.send_user_id = u1.id');
        $query->leftJoin('user u2', 'cs.receive_user_id = u2.id');
        $fields = 'cs.*, 
        u1.mobile as send_mobile, 
        u2.mobile as receive_mobile, 
        u1.avatar as send_avatar, 
        u2.avatar as receive_avatar, 
        u1.user_nickname as send_nickname, 
        u2.user_nickname as receive_nickname';

        $datas = $query->field($fields)
            ->where(function (Query $query) use ($filter) {
                $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
                $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
                if (!empty($startTime) && !empty($endTime) && $startTime >= $endTime) {
                    $tempTime = $startTime;
                    $startTime = $endTime;
                    $endTime = $tempTime;
                }
                if (!empty($startTime)) {
                    $query->where('cs.update_time', '>=', $startTime);
                }
                if (!empty($endTime)) {
                    $endTime = $endTime + 86400;
                    $query->where('cs.update_time', '<=', $endTime);
                }

                $userId = empty($filter['user_id']) ? 0 : intval($filter['user_id']);
                if (!empty($userId)) {
                    $query->where('cs.send_user_id', $userId);
                }

                $mobile = empty($filter['mobile']) ? 0 : intval($filter['mobile']);
                if (!empty($mobile)) {
                    $query->where('u1.mobile', $mobile);
                }

                if (!empty($filter['session_status']) && in_array($filter['session_status'], [1, 2])) {
                    $auditStatus = $filter['session_status'] == 1 ? 0 : 1;
                    $query->where('cs.session_status', $auditStatus);
                }
            })
            ->order('update_time', 'DESC')
            ->paginate(20);

        $this->assign('start_time', isset($filter['start_time']) ? $filter['start_time'] : '');
        $this->assign('end_time', isset($filter['end_time']) ? $filter['end_time'] : '');
        $this->assign('user_id', isset($filter['user_id']) ? $filter['user_id'] : '');
        $this->assign('mobile', isset($filter['mobile']) ? $filter['mobile'] : '');
        $this->assign('session_status', isset($filter['session_status']) ? $filter['session_status'] : '');

        $this->assign('page', $datas->render());
        $this->assign('items', $datas);
        return $this->fetch();
    }


}
